package testtiles;

public class Variable {

	private static final String EMPTY_STRING = "";
	
	private String name;
	private String string;
	private float value;
	private Variable next;

	public Variable(String name) {
		if (name == null) {
			throw new IllegalArgumentException("Argument 'name' is null");
		}
		name = name.trim();
		if (name == null) {
			throw new IllegalArgumentException("Argument 'name' is blank");
		}
		this.name = name;
	}
	
	public Variable(String name, String string) {
		this(name);
		setString(string);
	}
	
	public Variable(String name, float value) {
		this(name);
		setValue(value);
	}

	public String getName() {
		return name;
	}

	public String getString() {
		return string;
	}
	
	public float getValue() {
		return value;
	}
	
	public String name() {
		return name;
	}

	public String string() {
		return string;
	}
	
	public float value() {
		return value;
	}
	
	private float toFloat(String str) {
		if (str.matches("[-+]?[0-9]*\\.?[0-9]*")) {
			try {
				return Float.valueOf(str);
			} catch (NumberFormatException e) {
				// ignored
			}
		}
		return 0f;
	}

	public void setString(String string) {
		this.string = (string == null) ? EMPTY_STRING : string.trim();
		this.value = toFloat(this.string);
	}
	
	public void setValue(float value) {
		this.value = value;
		this.string = String.valueOf(value);
	}
	
	@Override
	public String toString() {
		return name + " \"" + string + "\" (" + value + ")";
	}
	
	// STATIC //
	private static Variable vars = null;
	
	public static Variable find(String name) {
		if (name == null) {
			return null;
		}
		name = name.trim();
		for (Variable var = vars; var != null; var = var.next) {
			if (name.equals(var.name)) {
				return var;
			}
		}
		return null;
	}
	
	public static void register(Variable var) {
		if (find(var.getName()) != null) {
			// TODO: DEBUG MESSAGE
			return;
		}
		var.next = vars;
		vars = var;
	}
	
	public static void set(String name, String string) {
		Variable var = find(name);
		if (var == null) {
			// TODO: DEBUG MESSAGE
			return;
		}
		var.setString(string);
	}
	public static void set(String name, float value) {
		Variable var = find(name);
		if (var == null) {
			// TODO: DEBUG MESSAGE
			return;
		}
		var.setValue(value);
	}
	
	public static String getString(String name) {
		Variable var = find(name);
		if (var == null) {
			// TODO: DEBUG MESSAGE
			return null;
		}
		return var.getString();
	}
	
	public static float getValue(String name) {
		Variable var = find(name);
		if (var == null) {
			// TODO: DEBUG MESSAGE
			return 0;
		}
		return var.getValue();
	}

	public static void main(String[] args) {
		System.out.println(new Variable("Debug", "1.1"));
		System.out.println(new Variable("FPS", "60"));
		System.out.println(new Variable("Name", "Jonh locker"));
		System.out.println(new Variable("XXX", null));
	}
	
	
}
